package com.timetask;

import java.io.File;
import java.util.Vector;

import com.bpms.common.CommonUtil;
import com.bpms.common.Constant;
import com.bpms.common.DateUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;

/**
 * 删除一周以上未完成支票交易的数据和影像
 * 
 * @author menlin
 * 
 */
public class DelChqueAndImgTask {
	private JdbcWorker jw;
	private String a_week_ago = DateUtil.getBeforeDay(Constant.CLEAR_DATA_PERIOD, "yyyy-MM-dd");

	/**
	 * 执行方法
	 */
	public void execute() {
		try {
			jw = new JdbcWorker();
			// 1.获取需要清理的数据
			Vector<VO> v = getData();
			// 2.清理影像
			if (v != null && 0 < v.size()) {
				for (VO vo : v) {
					File f = new File(vo.getAttr("IMAGE1").replace("-1001-", "-1002-"));
					if (f.exists())
						f.delete();
					f = new File(vo.getAttr("IMAGE2").replace("-1001-", "-1002-"));
					if (f.exists())
						f.delete();
					f = new File(vo.getAttr("IMAGE1"));
					if (f.exists())
						f.delete();
					f = new File(vo.getAttr("IMAGE2"));
					if (f.exists())
						f.delete();
				}
			}
			// 3.清理数据库数据
			StringBuilder sql = new StringBuilder(100);
			sql.append("DELETE cm_djb WHERE");
			sql.append(" createdate<'" + a_week_ago + "'");
			sql.append(" AND (flag='" + Constant.CM_DJB_FLAG_TMP + "'");
			sql.append(" OR flag='" + Constant.CM_DJB_FLAG_FAIL + "')");
			jw.execute(sql.toString());
			CommonUtil.debug("DelChqueImgTask清理过期数据sql=" + sql.toString());
		} catch (Exception e) {
			CommonUtil.debug("DelChqueImgTask exception", e);
		}
	}

	/**
	 * 获取CM_DJB flag 字段为0和8并且保存超过一周的数据
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private Vector<VO> getData() throws Exception {
		StringBuilder sql = new StringBuilder(100);
		sql.append("SELECT instanceid,flag,image1,image2 FROM cm_djb WHERE");
		sql.append(" createdate<'" + a_week_ago + "'");
		sql.append(" AND (flag='" + Constant.CM_DJB_FLAG_TMP + "'");
		sql.append(" OR flag='" + Constant.CM_DJB_FLAG_FAIL + "')");
		CommonUtil.debug("DelChqueImgTask获取过期数据sql=" + sql.toString());
		VOList list = jw.readAll(new VO(), sql.toString());
		return list.listData;

	}
}
